import serial

# 设置端口号，波特率，超时
com=serial.Serial('COM3',115200,timeout=0.5) #(Windows: 'COM3')
print(com.isOpen())
print(com.portstr)
com.flushInput()    #清除之前接收的全部数据

# 发送数据
value = [
    "001=1000"  #速度[0]
    ,"002=0"    #停止[1]
    ,"002=1"    #启动[2]
]
data = value[1]
data = (data + "\r\n").encode() #通信时一定加上 "\r\n"
print(data)
com.write(data)

#读取数据
#print(com.read(3))         # 读取三个字符
response = com.readall()    # 读取全部字符
print(response)
response = str(response, encoding = "utf-8") #byte转string
print(response)